package com.wsoft.system.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.wsoft.core.service.CommonMemberService;
import com.wsoft.core.utils.EmailUtil;
import com.wsoft.core.vo.MemberVO;
import com.wsoft.enums.TemplateTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 生日定时
 * @Author z001
 * @Date 2024/12/23 13:15
 */
@Slf4j
@Service
public class BirthdayJob implements Job {

    @Resource
    private CommonMemberService memberService;
    @Resource
    private EmailUtil emailUtil;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String now = DateUtil.format(DateUtil.date(), "yyyy-MM-dd");
        List<MemberVO> list = memberService.getMemberList();
        List<MemberVO> collect = list.stream().filter(item -> null != item.getBirthday() && now.equals(item.getBirthday().toString()) && StrUtil.isNotBlank(item.getEmail())).collect(Collectors.toList());
        if(CollUtil.isNotEmpty(collect)){
            collect .forEach(user->{
                emailUtil.sendEmail(user.getEmail(),
                        "生日祝福",
                        "亲，您有一份最真挚的祝福请您查收。在这个特殊的日子，公司全体同仁祝您生日快乐，幸福安康。感谢加盟，感恩有你，光辉之路，我们继续携手同行!",
                        TemplateTypeEnum.TEXT.getCode());
            });
        }
//        Asserts.fail("亲，您有一份最真挚的祝福请您查收。在这个特殊的日子，公司全体同仁祝您生日快乐，幸福安康。感谢加盟，感恩有你，光辉之路，我们继续携手同行!");

    }
}
